package com.ang.service2;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@ComponentScan(basePackages = "com.ang.service2")
//开启事务注解支持
@EnableTransactionManagement
public class Config2 {
    @Bean
    DataSource dataSource(){
        DriverManagerDataSource ds = new DriverManagerDataSource();

        ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
        ds.setUrl("jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC");
        ds.setUsername("root");
        ds.setPassword("ang12345");

        return ds;
    }

    @Bean
    TransactionManager transactionManager(){
        PlatformTransactionManager transactionManager = new DataSourceTransactionManager(dataSource());

        return transactionManager;
    }

    @Bean
    JdbcTemplate jdbcTemplate(){

        return new JdbcTemplate(dataSource());
    }
}
